package com.thug.ai.pathfinding;


public class Node {

	private int mX; // The coords of the current Node.
	private int mY;
	
	private float mCost; // Cost to move from any given Node to the current one. A cost of 0 indicates you cannot move to this node.
	private float mG; // Total accumulated Cost to current Node.
	private float mH; // Heuristic Value
	
	public boolean onClosedList; // These values save searching through the lists, increasing performance.
	public boolean onOpenList;
	
	private Node mParent; // Parent of this Node.
	private NodeMap mMapParent; // The NodeMap this Node belongs too.
	
	public Node(int x, int y, float cost, NodeMap mapParent)
    {
        mX = x;
        mY = y;
        mCost = cost;
        mMapParent = mapParent;

        onClosedList = false;
        onOpenList = false;
    }
	
	public Node[] getSurroundingNodes()
    {
        return mMapParent.getSurroundingNodes(mX, mY);
    }

    public float calcHeuristic(Node target)
    {
        return calcHeuristic(target.mX, target.mY);
    }

    public float calcHeuristic(int x, int y)
    {
        float H = mMapParent.calcHeuristic(mX, mY, x, y);
        if (H != -1)
        {
            return H * mCost;
        }
        else
        {
            throw new IllegalStateException("An error occured calculating the Heuristic.");
        }
    }
	
	@Override
	public String toString()
    {
        return String.format("Node[%S, %S] - [F, G, H] = [%S, %S, %S]", mX, mY, getF(), mG, mH);
        
    }
	
	@Override
	 public Node clone()
     {
         Node Copy = new Node(mX, mY, mCost, mMapParent);
         return Copy;
     }
	
	/* getter setters */
	public int getX() {
		return mX;
	}
	public void setX(int x) {
		this.mX = x;
	}
	public int getY() {
		return mY;
	}
	public void setY(int y) {
		this.mY = y;
	}
	public float getCost() {
		return mCost;
	}
	public void setCost(float cost) {
		this.mCost = cost;
	}
	/**
	 * Total accumulated Cost to current Node.
	 * @return
	 */
	public float getG() {
		return mG;
	}
	/**
	 * Total accumulated Cost to current Node.
	 * @param g
	 */
	public void setG(float g) {
		this.mG = g;
	}
	/**
	 * Heuristic Value
	 * @return
	 */
	public float getH() {
		return mH;
	}
	/**
	 * Heuristic Value
	 * @param h
	 */
	public void setH(float h) {
		this.mH = h;
	}
	public float getF() {
		return mG + mH;
	}
	public Node getParent() {
		return mParent;
	}
	public void setParent(Node parent) {
		this.mParent = parent;
	}
	public NodeMap getMapParent() {
		return mMapParent;
	}
	public void setMapParent(NodeMap mapParent) {
		this.mMapParent = mapParent;
	}
	
}
